package com.example.mobiledestroy.receiver;

import com.example.mobiledestroy.util.KeyFilter;
import com.example.mobiledestroy.util.MD5;
import com.example.mobiledestroy.util.SMSService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver{

	private String content = "";
	private String address = "";

	@Override
	public void onReceive(Context context, Intent intent) {
		if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
			abortBroadcast(); // 终止广播的继续传播
			Bundle bundle = intent.getExtras();
			Object[] obj = (Object[]) bundle.get("pdus");
			
			
			SmsMessage[] sms = new SmsMessage[obj.length];
			for (int i = 0; i < obj.length; i++) {
				sms[i] = SmsMessage.createFromPdu((byte[]) obj[i]);
			}
			
			SharedPreferences sharedata = context.getSharedPreferences("bind_num", 0);
			String data = sharedata.getString("mobile_num", null);
			
			//得到短信的发送者和内容
			for (SmsMessage s : sms) {
				//与存储的不一样
				if(!new MD5(s.getDisplayOriginatingAddress()).getMD5String().equals(data)){
					Log.i("","收到非绑定号码的信息");
					continue;
				}
				content = s.getDisplayMessageBody(); // 获得短信内容
				address = s.getDisplayOriginatingAddress();
				Log.i(address, content);
				
				content = s.getDisplayMessageBody(); // 获得短信内容
				Log.i(address, content);
				Toast.makeText(context, "绑定者：" + address + "\n 内容：" + content,
						Toast.LENGTH_LONG).show();
			}
			
			//将短信内容解析
			if(!content.equals("")){
				new KeyFilter(context,content,address).filter();
				
			}
		}
	}

}
